package com.bluemobi.ssm.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bluemobi.ssm.model.${toClassName};
<#if hasCompositePk&&primeType!=toClassName>import com.bluemobi.ssm.model.${toClassName}Id;</#if>
import com.bluemobi.ssm.model.common.DwzAjaxResult;
import com.bluemobi.ssm.model.common.${pagerType};
import com.bluemobi.ssm.service.${toClassName}Service;
import com.bluemobi.ssm.utils.JsonObject;

/**
* @ClassName: ${toClassName}类
* @Description: ${toClassName}
* @author
* @date
*/
@Controller
@RequestMapping("/administration/console/${alias}")
public class ${toClassName}Controller {

//装配${toClassName}Service
@Autowired
private ${toClassName}Service ${toEntityName}Service;

@RequestMapping("/list")
public String list(@ModelAttribute("pager") ${pagerType} pager,@ModelAttribute ${toClassName} ${toEntityName}, Model model){
long total = ${toEntityName}Service.total${toClassName}s(${toEntityName});
pager.setTotal(total);
${toEntityName}.setPager(pager);
List<${toClassName}> list = ${toEntityName}Service.get${toClassName}sByCondition(${toEntityName});
model.addAttribute("${toEntityName}List", list);
model.addAttribute("${toEntityName}Model", ${toEntityName});
model.addAttribute("pager", pager);
return "admin/${alias}/list";
}

@RequestMapping(value="/new",method=RequestMethod.GET)
public String new${toClassName}(){
return "admin/${alias}/new";
}

@RequestMapping(value="/save", method=RequestMethod.POST)
@ResponseBody
public String save${toClassName}(@ModelAttribute("${toEntityName}") ${toClassName} ${toEntityName}, BindingResult result) throws IOException{
DwzAjaxResult dwzResult;
if(result.hasErrors()){
dwzResult = new DwzAjaxResult("300", "参数错误", "${toEntityName}AddPage", "", "");
return new JsonObject(dwzResult).toString();
}
<#if hasFieldAutoIncrease>
if(null != ${toEntityName}Service.addNew${toClassName}(${toEntityName})){
dwzResult = new DwzAjaxResult("200", "保存成功", "${toEntityName}List", "closeCurrent", "");
} else {
dwzResult = new DwzAjaxResult("300", "操作失败，可能是系统错误", "${toEntityName}AddPage", "", "");
}
<#else>
${toEntityName}Service.addNew${toClassName}(${toEntityName});
dwzResult = new DwzAjaxResult("200", "保存成功", "${toEntityName}List", "closeCurrent", "");
</#if>
return new JsonObject(dwzResult).toString();
}

@RequestMapping(value="/edit",method=RequestMethod.GET)
public String edit${toClassName}(@RequestParam("id") <#if hasCompositePk>String<#else>${primeType}</#if> id, Model model){
<#if hasCompositePk>
${primeType} convertId = new ${primeType}();
//TODO 解析组合id串，并生成主键
</#if>
${toClassName} ${toEntityName} = ${toEntityName}Service.find${toClassName}ById(<#if hasCompositePk>convertId<#else>id</#if>);
model.addAttribute("${toEntityName}Model", ${toEntityName});
return "admin/${alias}/edit";
}

@RequestMapping(value="/info",method=RequestMethod.GET)
public String view${toClassName}(@RequestParam("id") <#if hasCompositePk>String<#else>${primeType}</#if> id, Model model){
<#if hasCompositePk>
${primeType} convertId = new ${primeType}();
//TODO 解析组合id串，并生成主键
</#if>
${toClassName} ${toEntityName} = ${toEntityName}Service.find${toClassName}ById(<#if hasCompositePk>convertId<#else>id</#if>);
model.addAttribute("${toEntityName}Model", ${toEntityName});
return "admin/${alias}/info";
}

@RequestMapping(value="/modify",method=RequestMethod.POST)
@ResponseBody
public String modify${toClassName}(@ModelAttribute("${toEntityName}") ${toClassName} ${toEntityName}, BindingResult result, Model model){
DwzAjaxResult dwzResult;
if(result.hasErrors()){
dwzResult = new DwzAjaxResult("300", "参数错误", "${toEntityName}AddPage", "", "");
return new JsonObject(dwzResult).toString();
}
${toEntityName}Service.modify${toClassName}(${toEntityName});
dwzResult = new DwzAjaxResult("200", "修改成功", "${toEntityName}List", "closeCurrent", "");
return new JsonObject(dwzResult).toString();
}

@RequestMapping(value="/remove")
@ResponseBody
public String remove${toClassName}(@RequestParam("id") <#if hasCompositePk>String<#else>${primeType}</#if> id){
<#if hasCompositePk>
${primeType} convertId = new ${primeType}();
//TODO 解析组合id串，并生成主键
</#if>
${toEntityName}Service.remove${toClassName}ById(<#if hasCompositePk>convertId<#else>id</#if>);
DwzAjaxResult result = new DwzAjaxResult("200", "删除成功", "${toEntityName}List", "", "");
return new JsonObject(result).toString();
}
}
